﻿using System.Numerics;
using System.Threading;
using System;
namespace Primer
{
    class Program
    {
        private const int IdleAt = 1000;
        private static readonly TumblerCollection _tumblers = new TumblerCollection();
        private static readonly ITracker _tracker = new Tracker(x => _tumblers.Add(ref x), Commit, Rollback);
        private static int _idle;

        static void Main(string[] args)
        {
            _tumblers.Read("Prime.dat");
            // _tumblers.Add(new BigInteger[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 });
            while (_tumblers.ID < 50) //int.MaxValue)
            {
                if (!_tracker.Run) { Console.Write("."); Thread.Sleep(500); continue; }
                var hit = _tumblers.Tumble();
                if (!hit || _idle++ == IdleAt)
                {
                    _idle = 0;
                    if (hit)
                        // idle
                        _tracker.Send(_tumblers.ID, 0, null);
                    else
                        // prime
                        _tracker.Send(_tumblers.ID, 1, new[] { _tumblers.Count.ToString() });
                }
            }
        }

        private static void Rollback(BigInteger v)
        {
        }

        private static void Commit(BigInteger v)
        {
        }
    }
}
